/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usfc.systemdistrib.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author usfch
 */
public class AnalyseurServer extends Thread {

    private ServerSocket serverSocket;
    PrintWriter out = null;
    BufferedReader in = null;

    public AnalyseurServer() {
    }

    @Override
    public void run() {
        try {
            System.out.println("run in server");
            serverSocket = new ServerSocket(1234);
            System.out.println("server is waiting for an incomig connection host= " + serverSocket.getInetAddress() + " port: 1234");
            Socket socket = serverSocket.accept();

            System.out.println("connection established" + "  socket" + socket.toString());
            //lets read and write using this connection

            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));
            String s = null;//=in.readLine();

            while ((s = in.readLine()) != null) {
                out.println(s);
                System.out.println("echo: " + in.readLine());
            }
            System.out.println(s);

            socket.close();
            System.out.println("Server is exciting");
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
            System.out.println("Could not listen on port: 4444");

        }
    }
}
